﻿using System.Data;
using System.Text;
using System.Windows.Forms;
using Ledict.ConditionExtract.Controls;

public class ComboBoxCheckBoxTreeView : ComboBox
{
    private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
    ToolStripControlHost treeViewHost;
    ToolStripDropDown dropDown;
    string tag = "true";
    StringBuilder nodesTag;
    StringBuilder nodesName=new StringBuilder();
    public ComboBoxCheckBoxTreeView()
    {
        TreeView treeView = new TreeView();
        treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
        treeView.BorderStyle = BorderStyle.None;
        treeView.CheckBoxes = true;
        treeView.AfterCheck += new TreeViewEventHandler(TreeView_AfterCheck); 
        treeViewHost = new ToolStripControlHost(treeView);
        dropDown = new ToolStripDropDown();
        dropDown.Width = this.Width;
        dropDown.Items.Add(treeViewHost);        
    }

    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Action != TreeViewAction.Unknown)
        {
            TreeNode node = e.Node;
            if (node.Checked == true)
            {
                node.ExpandAll();
            }
            if (node.Tag == null)
            {
                node.Tag = tag;//附加节点信息
            }
            else
            {
                node.Tag = null;
            }
            //   CheckAllChildNodes(e.Node, e.Node.Checked);

            //选中父节点
            //bool bol = true;
            //if (e.Node.Parent != null)
            //{
            //    for (int i = 0; i < e.Node.Parent.Nodes.Count; i++)
            //    {
            //        if (!e.Node.Parent.Nodes[i].Checked)
            //        {
            //            bol = false;
            //        }
            //    }
            //    e.Node.Parent.Checked = bol;

            //    if (bol)
            //    {
            //        e.Node.Parent.Tag = tag;
            //    }
            //    else
            //    {
            //        e.Node.Parent.Tag = null;
            //    }
            //}
            GetCheckNode();
            this.Text = GetCheckNode(); 
            //this.Text = e.Node.Text.ToString();
        }
    }

    private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
    {
        foreach (TreeNode node in treeNode.Nodes)
        {
            node.Checked = nodeChecked;
            node.Tag = nodeChecked?tag:null;
            if (node.Nodes.Count > 0)
            {
                this.CheckAllChildNodes(node, nodeChecked);
            }
        }
    }

    public string GetCheckNode()
    {
        nodesTag = new StringBuilder();
        TreeNode node = TreeView.Nodes[0];//得到treeview的根结点
        while (node.PrevNode != null)
        {
            node = node.PrevNode;
        }
        TraversNodes(node);//遍历根结点
        return nodesTag.ToString();
    }

    public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
        this.Text = TreeView.SelectedNode.Text.ToString ();
        TreeView.SelectedNode.Checked = true;
        TreeView_AfterCheck(sender, e);
        dropDown.Close();
    }
    public TreeView TreeView
    {
        get { return treeViewHost.Control as TreeView; }
    }
    private void ShowDropDown()
    {
        if (dropDown != null)
        {
            treeViewHost.Size = new System.Drawing.Size(DropDownWidth - 2, DropDownHeight);
            dropDown.Show(this, 0, this.Height);
        }
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
        {
            ShowDropDown();
            return;
        }
        base.WndProc(ref m);
    }

    private void InitializeComponent()
    {
            this.SuspendLayout();
            this.ResumeLayout(false);

    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (dropDown != null)
            {
                dropDown.Dispose();
                dropDown = null;
            }
        }
        base.Dispose(disposing);
    }

    /// <summary>
    /// 遍历方法
    /// </summary>
    /// <param name="parent"></param>
    private void TraversNodes(TreeNode parent)
    {
        TreeNode node = parent;
        if (node != null)
        {
            //if (node.Tag != null && node.Tag.ToString() == tag)
            //{
            if (node.Text != null && !string.IsNullOrEmpty(node.Text.ToString())&&node.Checked==true)
            {
                if (!nodesName.ToString().Contains(node.Text))
                {
                    string ss = node.Text.Replace(')', ' ');
                    nodesTag.AppendFormat("{0},", ss.Substring(ss.LastIndexOf('(') + 1));
                    string ss1 = node.Text.Replace('(', ' ');

                    nodesName.AppendFormat("{0},", node.Text.ToString());
                }
                else
                {
                    string ss = node.Text.Replace(')', ' ');
                    nodesTag.AppendFormat("{0},", ss.Substring(ss.LastIndexOf('(') + 1));
                }
            }
            if (node.FirstNode != null)//如果node节点还有子节点则进入遍历
            {
                TraversNodes(node.FirstNode);
            }
            if (node.NextNode != null)//如果node节点后面有同级节点则进入遍历
            {
                TraversNodes(node.NextNode);
            }
        }
    }


}
